module.exports = function (grunt) {
    var jsSrcFileArray = [
        'src/js/common/*.js',
        'src/js/*/*.js',
        'src/js/*/services/*.js',
        'src/js/*/directives/*.js',
        'src/js/*/controllers/*.js',
        'src/js/app.js',
        '!src/js/config/prod.js'
    ];

    var jsScriptTags = function (srcPattern, destPath) {
        if (srcPattern === undefined) {
            throw new Error("srcPattern undefined");
        }
        if (destPath === undefined) {
            throw new Error("destPath undefined");
        }
        return grunt.util._.reduce(
            grunt.file.expandMapping(srcPattern, destPath, {
                filter: 'isFile',
                flatten: true,
                expand: true,
                cwd: '.'
            }),
            function (sum, file) {
                return sum + '\n<script src="' + file.src[0] + '" type="text/javascript"></script>';
            },
            ''
        );
    };

    var getVersion = function() {
      return Date.now();
    };

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        jsSrcFileArray: jsSrcFileArray,
        jsScriptTags: jsScriptTags,
        getVersion: getVersion,
        replace: {
            app: {
                src: ['src/js/app.js'],
                dest: 'src/js/app-prod.js',
                replacements: [{
                    from: '/*devstart*/',
                    to: '/*devstart'
                }, {
                    from: '/*devend*/',
                    to: 'devend*/'
                }, {
                    from: '/*productstart',
                    to: '/*productstart*/'
                }, {
                    from: 'productend*/',
                    to: '/*productend*/'
                }
                ]
            }
        },

        html2js: {
            options: {
                //The prefix relative to the project directory that should be stripped from each template path
                //to produce a module identifier for the template. For example, a template located at
                //src/projects/projects.tpl.html would be identified as just projects/projects.tpl.html.
                base: '.'
            },
            main: {
                src: ['partials/**/*.html'],
                dest: 'partials/templates.js',
                module: 'app.templates'
            },
        },
        concat: {
            js: {
                src: ['libs/fastclick/fastclick.js', 'libs/*.js',
                    'libs/chart.js-2.1.6/*.js',
                    'libs/jquery/*.js', 'libs/jquery/plugins/**/*.js',
                    'libs/bootstrap-*/**/*.min.js', 'libs/lte-2.3.3/js/app.js',
                    'libs/angular/*.js', 'libs/angular/plugins/**/*.js'],
                dest: 'dist/js/vendor.min.js'
            },
            css: {
                src: ['libs/bootstrap-*/**/*.min.css', 'libs/font-awesome-*/**/*.min.css',
                    'libs/lte-*/css/*.min.css', 'libs/lte-*/css/skins/skin-blue.min.css',
                    'libs/angular/**/*.css'],
                dest: 'dist/css/vendor.min.css'
            }
        },
        cssmin: {
            prod: {
                files: [{
                    expand: true,
                    cwd: 'src/css/',
                    src: ['*.css'],
                    dest: 'dist/css',
                    ext: '.min.css'
                }]
            }
        },
        uglify: {
            prod: {
                files: {
                    'dist/js/app.min.js': ['partials/templates.js', 'src/js/*/*.js', 'src/js/*/services/*.js',
                        'src/js/*/directives/*.js', 'src/js/*/controllers/*.js', 'src/js/app-prod.js',
                        '!src/js/config/dev.js', '!src/js/app.js']
                }
            }
        },
        includereplace: {
            dev: {
                options: {
                    globals: {
                        cssTag: '<link rel="stylesheet" href="src/css/app.css">',
                        scriptsTags: '<%= jsScriptTags(jsSrcFileArray, "./")%>'
                    }
                },
                src: [
                    'src/index.html',
                    'src/login.html'
                ],
                dest: './',
                flatten: true,
                cwd: '.',
                expand: true
            },
            prod: {
                options: {
                    globals: {
                        cssTag: '<link rel="stylesheet" href="dist/css/app.min.css?<%=getVersion()%>">',
                        scriptsTags: '<script src="dist/js/app.min.js?<%=getVersion()%>" type="text/javascript"></script>'
                    }
                },
                src: [
                    'src/index.html',
                    'src/login.html'
                ],
                dest: './',
                flatten: true,
                cwd: '.',
                expand: true
            }
        },
        watch: {
            src: {
                files: ['src/*.html', 'src/js/**/*.js', 'src/css/**/*.css'],
                tasks: ['default']
            }
        },
        copy: {
            main: {
                files: [
                    // includes files within path and its sub-directories
                    {
                        expand: true,
                        src: ['api/**', "dist/**", "img/**", "index.html", "login.html"],
                        dest: 'release/order_wap/'
                    }
                ]
            }
        }
    });

    grunt.loadNpmTasks('grunt-text-replace');
    grunt.loadNpmTasks('grunt-html2js');
    grunt.loadNpmTasks("grunt-contrib-concat");
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-include-replace');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-copy');

    grunt.registerTask('default', ['concat', 'includereplace:dev']);
    grunt.registerTask('prod', ['replace', 'html2js', 'concat', 'cssmin', 'uglify', 'includereplace:prod', "copy"]);
};